package com.xidian.zhiancloud.service.dangerWorkTicket;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.ReviewWorkBeforeMapper;
import com.xidian.zhiancloud.model.workSupervise.ReviewWorkBefore;
import com.xidian.zhiancloud.model.dangerWorkTicket.WorkTicket;
import com.xidian.zhiancloud.service.dangerWorkTicket.WorkTicket.WorkTicketService;
import com.xidian.zhiancloud.service.dangerWorkTicket.WorkTicket.WorkTicketTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class ReviewWorkBeforeService extends ServiceImpl<ReviewWorkBeforeMapper, ReviewWorkBefore> implements IService<ReviewWorkBefore> {

    @Autowired
    WorkTicketTemplateService workTicketTemplateService;

    @Autowired
    WorkTicketService workTicketService;

    /**
     * 返回Map<risk, 是否同意>
     *
     * @param reviewWorkBefore
     * @return
     */
    public Map<String, Integer> getRiskAssess(ReviewWorkBefore reviewWorkBefore) {
        Map<String, Integer> riskAssess = new HashMap<>();
        Long ticketId = reviewWorkBefore.getRelateWorkId();
        WorkTicket workTicket = workTicketService.getById(ticketId);
        List<String> riskList = workTicketTemplateService.riskList(workTicket.getContent());
        String opinionStr = reviewWorkBefore.getOpinion();
        List<Integer> opinionList = riskList(opinionStr);
        for (int i = 0; i < riskList.size(); i++) {
            riskAssess.put(riskList.get(i), opinionList.get(i));
        }
        return riskAssess;
    }

    /**
     * 分割是否同意
     * @param opinionStr
     * @return
     */
    public List<Integer> riskList(String opinionStr) {
        List<Integer> opinions = new LinkedList<>();
        for(String opinion : opinionStr.split(",")) {
            opinions.add(Integer.parseInt(opinion));
        }
        return opinions;
    }
}
